package edu.snake.implement.model.character;

import edu.snake.core.model.Character;
import edu.snake.core.model.Position;

import java.util.Deque;
import java.util.LinkedList;

import static edu.snake.implement.factory.PositionFactory.getNewPosition;


public abstract class CharacterImpl implements Character {

    protected Position position;
    protected int score;

    public CharacterImpl(Position position) {
        this.position = getNewPosition(position);
        this.score = 0;
    }

    public CharacterImpl(Position position, int score) {
        this.position = getNewPosition(position);
        this.score = score;
    }

    @Override
    public synchronized void setPosition(Position position) {
        this.position = getNewPosition(position);
    }

    @Override
    public synchronized Position getPosition() {
        return getNewPosition(position);
    }

    @Override
    public Deque<Character> getAllElement() {
        return new LinkedList<Character>();
    }

    @Override
    public void setAllElement(Deque<Character> characters) {
        //todo throw unsupportException???
    }

    @Override
    public int getScore() {
        return score;
    }

    @Override
    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CharacterImpl character = (CharacterImpl) o;

        if (score != character.score) return false;
        if (position != null ? !position.equals(character.position) : character.position != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = position != null ? position.hashCode() : 0;
        result = 31 * result + score;
        return result;
    }
}
